package cn.mw.cmdb.timeplan.controller;

import cn.mw.cmdb.enums.OperatorTypeDescEnum;
import cn.mw.cmdb.timeplan.entity.MwTimePlan;
import cn.mw.cmdb.timeplan.entity.MwTimePlanOption;
import cn.mw.cmdb.timeplan.entity.MwTimePlanRelationModuleDto;
import cn.mw.cmdb.timeplan.param.MwTimePlanParam;
import cn.mw.cmdb.timeplan.service.MwTimePlanService;
import cn.mw.cmdb.view.ViewConvertContext;
import cn.mw.cmdb.view.ViewManager;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.log.client.LogClientSimple;
import cn.mw.log.enums.BusinessLogType;
import cn.mw.log.enums.LogLevel;
import cn.mw.microMonitorCommon.api.common.BaseApiService;
import cn.mw.microMonitorCommon.api.common.ResponseBase;
import cn.mw.microMonitorCommon.constant.Constants;
import cn.mw.microMonitorCommon.security.utils.SecurityUtils;
import cn.mw.microMonitorCommon.util.DateUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/timePlanManage")
@Controller
@Tag(name = "时间计划管理")
@Slf4j
public class MwTimePlanController extends BaseApiService {

    private MessageSource messageSource;

    private MwTimePlanService mwTimePlanService;

    private ViewManager viewManager;

    private LogClientSimple logClientSimple;

    private final String MENU_NAME = "时间计划";

    public MwTimePlanController(MessageSource messageSource , MwTimePlanService mwTimePlanService,ViewManager viewManager,
                                LogClientSimple logClientSimple){
        this.messageSource = messageSource;
        this.mwTimePlanService = mwTimePlanService;
        this.viewManager = viewManager;
        this.logClientSimple = logClientSimple;
    }

    @PostMapping("/listOption")
    @ResponseBody
    @Operation(summary = "下拉框数据")
    public ResponseBase listOption() throws Exception {
        MwTimePlanOption mwTimePlanOption = new MwTimePlanOption();
        mwTimePlanOption.init(messageSource);
        return setResultSuccess(mwTimePlanOption);
    }

    @PostMapping("/save")
    @ResponseBody
    @Operation(summary = "保存时间计划")
    public ResponseBase save(@RequestBody MwTimePlanParam mwTimePlanParam) throws Exception {
        Boolean flag = true;
        if(StringUtils.isNotEmpty(mwTimePlanParam.getId())){
            flag = false;
        }
        MwTimePlanParam ret = mwTimePlanService.save(mwTimePlanParam);
        if(!flag){
            logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.SYSTEM_MANAGE.getId(),
                    messageSource.getMessage("mwCmdb-operation-log", new Object[]{ SecurityUtils.getUser().getNickName(), DateUtils.now()
                            , BusinessLogType.SYSTEM_MANAGE.getCnName(), OperatorTypeDescEnum.update.getCname() + MENU_NAME,
                            OperatorTypeDescEnum.update.getCname() + mwTimePlanParam.getName(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));
        }else{
            logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.SYSTEM_MANAGE.getId(),
                    messageSource.getMessage("mwCmdb-operation-log", new Object[]{ SecurityUtils.getUser().getNickName(), DateUtils.now()
                            , BusinessLogType.SYSTEM_MANAGE.getCnName(), OperatorTypeDescEnum.add.getCname() + MENU_NAME,
                            OperatorTypeDescEnum.add.getCname() + mwTimePlanParam.getName(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));
        }
        return setResultSuccess(ret);
    }

    @PostMapping("/select")
    @ResponseBody
    @Operation(summary = "查看时间计划")
    public ResponseBase select(@RequestBody MwTimePlanParam mwTimePlanParam) throws Exception {
        MwTimePlanParam ret = mwTimePlanService.select(mwTimePlanParam);
        return setResultSuccess(ret);
    }

    @PostMapping("/list")
    @ResponseBody
    @Operation(summary = "查看时间计划")
    public ResponseBase list(@RequestBody SearchParam searchParam) throws Exception {
        List<MwTimePlanParam> ret = mwTimePlanService.list(searchParam);
        Object pageRet = viewManager.getListViewObject(ret, MwTimePlanParam.class, searchParam, new ViewConvertContext());
        return setResultSuccess(pageRet);
    }

    @PostMapping("/delete")
    @ResponseBody
    @Operation(summary = "删除时间计划")
    public ResponseBase delete(@RequestBody MwTimePlanParam timePlanParam) throws Exception {
        List<MwTimePlan> timePlans = mwTimePlanService.selectListByIds(timePlanParam);
        mwTimePlanService.delete(timePlanParam);
        if(CollectionUtils.isEmpty(timePlans)){
            return setResultSuccess();
        }
        List<String> collect = timePlans.stream().map(s -> s.getName()).collect(Collectors.toList());
        String name = String.join(",", collect);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.SYSTEM_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.SYSTEM_MANAGE.getCnName(), OperatorTypeDescEnum.remove.getCname() + MENU_NAME,
                        OperatorTypeDescEnum.remove.getCname() + name, Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));
        return setResultSuccess();
    }


    @PostMapping("/getDetails")
    @ResponseBody
    @Operation(summary = "时间计划查看详情")
    public ResponseBase<List<MwTimePlanRelationModuleDto>> getTimePlanDetails(@RequestBody MwTimePlanParam timePlanParam) throws Exception {
        List<MwTimePlanRelationModuleDto> detailInfo = mwTimePlanService.getTimePlanDetailInfo(timePlanParam);
        Object listViewObject = viewManager.getListViewObject(detailInfo, MwTimePlanRelationModuleDto.class, timePlanParam, new ViewConvertContext());
        return setResultSuccess(listViewObject);
    }
}
